<template>
    <div>
        <div class="FindView">
            <div class="header">
                <div class="head">
                    <input type="text" @keyup.enter="onword" v-model="word">
                </div>

                <div class="Cancel" @click="prev">
                    取消
                </div>
            </div>

            <div v-if="!word">
                <div class="hotbox">
                    <p>热门搜索</p>
                    <div class="itembox">
                        <div class="item" v-for="item in hot" :key="item.id">
                            <span>{{ item.hot + 1 }}</span>{{ item.name }}
                        </div>
                    </div>


                </div>


                <div class="history-search">
                    <div class="search-box">
                        <div class="title">历史搜索</div>

                        <span class="del" @click="del">
                            <svg t="1668357209634" class="icon" viewBox="0 0 1024 1024" version="1.1"
                                xmlns="http://www.w3.org/2000/svg" p-id="4843" width="200" height="200">
                                <path
                                    d="M840 288H688v-56c0-40-32-72-72-72h-208C368 160 336 192 336 232V288h-152c-12.8 0-24 11.2-24 24s11.2 24 24 24h656c12.8 0 24-11.2 24-24s-11.2-24-24-24zM384 288v-56c0-12.8 11.2-24 24-24h208c12.8 0 24 11.2 24 24V288H384zM758.4 384c-12.8 0-24 11.2-24 24v363.2c0 24-19.2 44.8-44.8 44.8H332.8c-24 0-44.8-19.2-44.8-44.8V408c0-12.8-11.2-24-24-24s-24 11.2-24 24v363.2c0 51.2 41.6 92.8 92.8 92.8h358.4c51.2 0 92.8-41.6 92.8-92.8V408c-1.6-12.8-12.8-24-25.6-24z"
                                    p-id="4844"></path>
                                <path
                                    d="M444.8 744v-336c0-12.8-11.2-24-24-24s-24 11.2-24 24v336c0 12.8 11.2 24 24 24s24-11.2 24-24zM627.2 744v-336c0-12.8-11.2-24-24-24s-24 11.2-24 24v336c0 12.8 11.2 24 24 24s24-11.2 24-24z"
                                    p-id="4845"></path>
                            </svg>
                        </span>
                    </div>
                    <ul class="history-list">
                        <li class="history-item" v-for="(item, index) in historyList" :key="index"
                            @click="goCookbook, (ks = item)">
                            {{ item }}</li>
                    </ul>
                </div>




            </div>
            
            <div class="bigbook" v-if="word">
                <div class="book" v-for="(item) in booklists" :key="item.id">
                    <div class="img">
                        <img :src="item.cover" alt="">
                    </div>
                    <div class="textbox">
                        <div class="titlebox">
                            <div clsss="title">
                                {{ item.title }}
                            </div>
                            <p>{{ item.author }}</p>
                        </div>

                        <div class="briefly">
                            {{ item.summary }}
                        </div>

                    </div>
                </div>

            </div>
        </div>
    </div>
</template>

<script>
import { getHotData } from "../api/search.js"
import { getkeywords } from "../api/search.js"
export default {

    data() {
        return {
            hot: [],
            word: "",
            historyList: [],
            booklists:[],
            lists:[],
            history:[]
        }
    },



    methods: {
        // 返回上一级
        prev() {
            this.$router.go(-1)
        },


        // 热门搜索
        getHotDataFun() {
            getHotData().then((data) => {
                // console.log("hot",data)
                this.hot = data.list.reverse()
                console.log("hot", this.hot)
            })
        },


        // 搜索数据
        getkeywordsFun() {
            getkeywords({ keyword: this.word }).then((data => {
                console.log("想吃你", data)
                this.booklists =data.data.books
                console.log("booklists",this.booklists)
            }))
        },


        // 键盘输入事件
        onword(event) {

            this.word = event.target.value
            console.log(this.word)
            this.lists.push(this.word)
            window.localStorage.setItem("history", JSON.stringify(this.lists))
            this.historyList = JSON.parse(window.localStorage.getItem("history"))
        },



        // 删除本地
        del() {
        //     window.localStorage.setItem("history", JSON.stringify(this.history))
          this.historyList = [];
        window.localStorage.removeItem("history")

        },

    },


    // 侦听器
    watch: {
        word: function () {
            this.getkeywordsFun()
        }
    },



// 调用方法
    created() {
        this.getHotDataFun()
        this.getkeywordsFun()

      
        // this.historyList = JSON.parse(window.localStorage.getItem("history"))
        // console.log( this.historyList )
    },

    


}
</script>

<style lang="scss" scoped>
.FindView {
    .header {

        display: flex;
        padding: 10px;
        align-items: center;
        background-color: #fff;

        .head {
            flex: 1;
            padding-left: 20px;
            padding-top: 5px;
            padding-bottom: 5px;
            margin-right: 20px;
            background: url(../assets/img/搜索.png) no-repeat;
            background-size: 15px 15px;

            background-color: #f0f0f0;
            border-radius: 20px;
            background-position: 7px 8px;

            input {
                background: none;
                outline: none;
                border: 0px;
                padding-left: 8px;
            }
        }

        .Cancel {
            font-size: 15px;
            color: #808080;
        }
    }

    .hotbox {
        padding-top: 10px;
        padding-left: 10px;

        p {
            color: #817b74;
            margin-bottom: 10px;
            font-size: 15px;
        }

        .itembox {
            display: flex;
            flex-wrap: wrap;

            .item {
                display: flex;
                flex: 0 0 50%;
                flex-wrap: wrap;
                align-items: center;

                span {
                    width: 14px;
                    height: 14px;
                    text-align: center;
                    border-radius: 100%;
                    background-color: #e64d2e;
                    color: #ffffff;
                    font-size: 12px;
                    margin-right: 4px;
                }


                color: #0b0b0b;
                font-size: 12px;

            }
        }
    }

    .search-box {
        padding: 0px 10px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        align-items: flex-end;
        font-size: 14px;
        color: #817b74;

        .title {
            margin-top: 20px;
            font-weight: 700;
        }

        .del {
            width: 25px;
            height: 25px;

            .icon {
                width: 25px;
                height: 25px;
            }
        }

    }

    .bigbook {
        background-color: #ffffff;
        padding: 0px 10px;
        padding-top: 10px;
        padding-bottom: 6px;

        .book {
            display: flex;

            margin-bottom: 20px;

            .img {
                img {
                    width: auto;
                    height: 100px;
                }

                margin-right: 12px;
            }

            .textbox {
                display: flex;
                flex-direction: column;
                flex: 1;

                .titlebox {
                    margin-bottom: 33px;

                    .title {
                        font-size: 14px;
                        color: #000;
                        font-weight: bold;
                        margin-bottom: 9px;
                    }

                    p {
                        font-size: 12px;
                        color: #cccccc;
                    }
                }


                .briefly {
                    width: 274px;
                    // height: 37px;
                    overflow: hidden;
                    // flex-wrap: wrap;
                    word-wrap: break-word;
                    text-overflow: ellipsis;
                    display: -webkit-box;
                    -webkit-line-clamp: 2;
                    -webkit-box-orient: vertical;
                    font-size: 12px;
                }
            }




        }
    }

    .history-list{
        display: flex;
        .history-item{
            padding-left: 10px;
            margin-right: 4px;
            font-size: 12px;
            color: #817b74;
        }
    }
}
</style>